Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Text
Imports System.Collections.Generic
Imports ASPNET.StarterKit.BusinessLogicLayer

Partial Public Class Report_Resources_aspx
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        If (Not Page.IsPostBack) Then
            If Page.User.IsInRole("ProjectAdministrator") Then
                ProjectData.SelectMethod = "GetAllProjects"
            Else
                ProjectData.SelectParameters.Add(New Parameter("userName", TypeCode.String, Page.User.Identity.Name))
                ProjectData.SelectParameters.Add(New Parameter("sortParameter", TypeCode.String, String.Empty))
                ProjectData.SelectMethod = "GetProjectsByManagerUserName"
            End If
            ProjectList.DataBind()

            UserData.SelectParameters.Add(New Parameter("userNames", TypeCode.String, BuildValueList(ProjectList.Items, False)))
            UserData.SelectMethod = "GetProjectMembers"

            Dim startingDate As DateTime = DefaultValues.GetDateTimeMinValue()
            Dim endingDate As DateTime = DateTime.Now
            StartDate.Text = startingDate.ToString("d")
            EndDate.Text = endingDate.ToString("d")
        End If

    End Sub

    Protected Function BuildValueList(ByVal items As ListItemCollection, ByVal itemMustBeSelected As Boolean) As String

        Dim idList As StringBuilder = New StringBuilder()
        For Each item As ListItem In items
            If itemMustBeSelected AndAlso (Not item.Selected) Then
                Continue For
            Else
                idList.Append(item.Value.ToString())
                idList.Append(",")
            End If
        Next item

        Return idList.ToString()

    End Function

    Protected Sub GenResourceRpt_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        UserListRequiredFieldValidator.Validate()

        If UserListRequiredFieldValidator.IsValid Then
            Session.Add("SelectedUserNames", BuildValueList(UserList.Items, True))
            Session.Add("SelectedStartingDate", StartDate.Text)
            Session.Add("SelectedEndDate", EndDate.Text)
            Response.Redirect("Report_Resources_Result.aspx")
        End If

    End Sub

End Class
